<template>
  <BasicModal
    v-bind="$attrs"
    :title="editData.remarkType === RemarkTypeEnum.schRemark ? '中学教师评语' : '高校教师评语'"
    @ok="handleSubmit"
    @register="registerModal"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  import { BasicModal, useModalInner } from "/@/components/Modal";
  import { BasicForm, useForm } from "/@/components/Form";
  import { remarkFormSchema } from "/@/views/teacher/practical/member/member.data";
  import { defaultModalFormSettings } from "/@/hooks/core/useDefault";
  import { useMessage } from "/@/hooks/web/useMessage";
  import { setGxRemarkApi, setZxRemarkApi } from "/@/api/core/internInfo";
  import { RemarkTypeEnum } from "/@/enums/typeEnum";

  const editData = ref<any>({});

  const [registerForm, { validate, setFieldsValue }] = useForm({
    schemas: remarkFormSchema,
    ...defaultModalFormSettings,
  });

  const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner((data) => {
    editData.value = data;
    setFieldsValue({ content: data.content });
  });

  const { createMessage } = useMessage();

  async function handleSubmit() {
    try {
      const { content } = await validate();
      changeLoading(true);
      changeOkLoading(true);
      switch (editData.value.remarkType) {
        case RemarkTypeEnum.schRemark: {
          await setZxRemarkApi(editData.value.userId, content);
          createMessage.success("评价成功");
          break;
        }
        case RemarkTypeEnum.univRemark: {
          await setGxRemarkApi(editData.value.userId, content);
          createMessage.success("评价成功");
          break;
        }
      }
      closeModal();
    } finally {
      changeLoading(false);
      changeOkLoading(false);
    }
  }
</script>
